package WaitNotify;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UseWaitAndNotifyThree {
    static final Object room = new Object();
    static boolean hasCigarette = false;//有没有烟
    static boolean hasTakeout = false;//有没有外卖

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (room) {
                while (!hasCigarette) {
                    log.debug("没烟，先歇会！");
                    try {
//                        Thread.sleep(2000);
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    log.debug("有烟没？[{}]", hasCigarette);
                    if (hasCigarette) {
                        log.debug("可以开始干活了");
                    }
                }
            }
        }, "小南").start();

        new Thread(() -> {

            synchronized (room) {
                while (!hasTakeout) {
                    log.debug("外卖没到，先歇会！");
                    try {
//                        Thread.sleep(2000);
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    log.debug("外卖到嘛了？[{}]", hasTakeout);
                    if (hasTakeout) {
                        log.debug("可以开始干活了");
                    }
                }
            }

        }, "小女").start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(() -> {
            synchronized (room) {
                hasTakeout = true;
                log.debug("外卖到了");
                room.notifyAll();
            }
        }, "外卖员").start();

    }
}
